/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.DAO.CustomerDAO;
import model.entities.Customer;
import model.entities.Services;

/**
 *
 * @author Hieu.Nguyen
 */
public class FrmCustomerManagement extends javax.swing.JPanel {

    /**
     * Creates new form QuanLyNhanVien
     */
    public FrmCustomerManagement() {
        initComponents();
        setEnableComponent(true);
        loadTableCustomer(refreshCustomerList());
    }
    
    public List<Customer> refreshCustomerList() {
        
       List<Customer> customerList;
       customerList = model.DAO.CustomerDAO.getInstance().getCustomer(true);

        return customerList;
    }

    public void loadTableCustomer(List<Customer> list) {
        DefaultTableModel dtm = (DefaultTableModel) tblCustomerMgmt.getModel();
        String[] colNames = {"CustomerID", "FirstName","MiddleName","LastName","AddStreet","AddCity","AddState","AddZip","AddCountry","HomePhone","WorkPhone","Email","LastVisited","CCNumber","CCExpiry"};
        dtm.setColumnIdentifiers(colNames);
        dtm.setNumRows(0);

        for (Customer stud : list) {
            String[] cells = new String[15];
            cells[0] = String.valueOf(stud.getCustomerID());
            cells[1] = stud.getFirstName();
            cells[2] = stud.getMiddleName();
            cells[3] = stud.getLastName();
            cells[4] = stud.getAddStreet();
            cells[5] = stud.getAddCity();
            cells[6] = stud.getAddState();
            cells[7] = String.valueOf(stud.getAddZip());
            cells[8] = stud.getAddCountry();
            cells[9] = String.valueOf(stud.getHomePhone());
            cells[10] = String.valueOf(stud.getWorkPhone());
            cells[11] = stud.getEmail();
            cells[12] = String.valueOf(stud.getLastVisited());
            cells[13] = String.valueOf(stud.getcCNumber());
            cells[14] = String.valueOf(stud.getcCExpiry());
            

            dtm.addRow(cells);
        }
    }
    public void binding() {
        try {
            int rowIndex = tblCustomerMgmt.getSelectedRow();

            String CustomerID = (String)tblCustomerMgmt.getValueAt(rowIndex, 0);
            txtCustomerID.setText(CustomerID);
            
            String firstname = (String) tblCustomerMgmt.getValueAt(rowIndex, 1);
            txtFirstName.setText(firstname);
            
            String middlename = (String) tblCustomerMgmt.getValueAt(rowIndex, 2);
            txtMiddleName.setText(middlename);
            
            String lastname = (String) tblCustomerMgmt.getValueAt(rowIndex, 3);
            txtLastName.setText(lastname);
            
            String street = (String) tblCustomerMgmt.getValueAt(rowIndex, 4);
            txtAddStreet.setText(street);
            
            String city = (String) tblCustomerMgmt.getValueAt(rowIndex, 5);
            txtAddCity.setText(city);
            
            String state = (String) tblCustomerMgmt.getValueAt(rowIndex, 6);
            txtAddState.setText(state);
            
            String zip = (String) tblCustomerMgmt.getValueAt(rowIndex, 7);
            txtAddZip.setText(zip);
            
            String country = (String) tblCustomerMgmt.getValueAt(rowIndex, 8);
            txtAddCountry.setText(country);
            
            String homephone = (String) tblCustomerMgmt.getValueAt(rowIndex, 9);
            txtHomePhone.setText(homephone);
            
            String workphone = (String) tblCustomerMgmt.getValueAt(rowIndex, 10);
            txtWorkPhone.setText(workphone);
            
            String email = (String) tblCustomerMgmt.getValueAt(rowIndex, 11);
            txtEmail.setText(email);
            
            String lastvisited = (String) tblCustomerMgmt.getValueAt(rowIndex, 12);
            txtLastVisited.setText(lastvisited);
            
            String ccnumber = (String) tblCustomerMgmt.getValueAt(rowIndex, 13);
            txtCCNumber.setText(ccnumber);
            
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            if (tblCustomerMgmt.getValueAt(rowIndex, 14) != null) {
                dtCCExpiry.setDate(df.parse((String) tblCustomerMgmt.getValueAt(rowIndex, 14)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setEnableComponent(boolean isTrue) {
        txtCustomerID.setEnabled(false);
        txtFirstName.setEnabled(isTrue);
        txtMiddleName.setEnabled(isTrue);
        txtLastName.setEnabled(isTrue);
        txtAddStreet.setEnabled(isTrue);
        txtAddCity.setEnabled(isTrue);
        txtAddState.setEnabled(isTrue);
        txtAddZip.setEnabled(isTrue);
        txtAddCountry.setEnabled(isTrue);
        txtHomePhone.setEnabled(isTrue);
        txtWorkPhone.setEnabled(isTrue);
        txtEmail.setEnabled(isTrue);
        txtLastVisited.setEnabled(isTrue);
        txtCCNumber.setEnabled(isTrue);
        dtCCExpiry.setEnabled(isTrue);
        btnCanel.setEnabled(isTrue);
        btnDelete.setEnabled(isTrue);
        btnEdit.setEnabled(isTrue);
        btnSave.setEnabled(isTrue);
        
        
    }   
    void resetComponent() {
        txtFirstName.setText("");
        txtMiddleName.setText("");
        txtLastName.setText("");
        txtAddStreet.setText("");
        txtAddCity.setText("");
        txtAddState.setText("");
        txtAddZip.setText("");
        txtAddCountry.setText("");
        txtHomePhone.setText("");
        txtWorkPhone.setText("");
        txtEmail.setText("");
        txtCCNumber.setText("");
        txtCustomerID.setText("");
        txtLastVisited.setText("");
 
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCustomerMgmt = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtCustomerID = new javax.swing.JTextField();
        txtFirstName = new javax.swing.JTextField();
        txtMiddleName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtAddStreet = new javax.swing.JTextField();
        txtAddCity = new javax.swing.JTextField();
        txtAddState = new javax.swing.JTextField();
        txtAddZip = new javax.swing.JTextField();
        txtAddCountry = new javax.swing.JTextField();
        txtHomePhone = new javax.swing.JTextField();
        txtWorkPhone = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtLastVisited = new javax.swing.JTextField();
        txtCCNumber = new javax.swing.JTextField();
        dtCCExpiry = new com.toedter.calendar.JDateChooser();
        btnSave = new javax.swing.JButton();
        btnCanel = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("Customer Management");

        btnAdd.setText("AddCustomer");
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDeleteMouseClicked(evt);
            }
        });

        btnEdit.setText("Edit Customer");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        tblCustomerMgmt.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "CustomerID", "CustomerName", "Address", "Home Phone", "Work Phone", "CMND", "Zip", "Country", "Email", "LastVisited", "CCNumber"
            }
        ));
        tblCustomerMgmt.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCustomerMgmtMouseClicked(evt);
            }
        });
        tblCustomerMgmt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblCustomerMgmtKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblCustomerMgmt);

        jLabel2.setText("CustomerID :");

        jLabel3.setText("FirstName    :");

        jLabel4.setText("MiddleName :");

        jLabel5.setText("LastName    :");

        jLabel6.setText("AddStreet   :");

        jLabel7.setText("AddCity      :");

        jLabel8.setText("AddState     :");

        jLabel9.setText("AddZip         :");

        jLabel10.setText("AddCountry :");

        jLabel11.setText("HomePhone :");

        jLabel12.setText("WorkPhone  :");

        jLabel13.setText("Email            :");

        jLabel14.setText("LastVisited :");

        jLabel15.setText("CCNumber :");

        jLabel16.setText("CCExpiry   :");

        btnSave.setText("Save");
        btnSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSaveMouseClicked(evt);
            }
        });

        btnCanel.setText("Canel");
        btnCanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCustomerID)
                            .addComponent(txtFirstName)
                            .addComponent(txtMiddleName)
                            .addComponent(txtLastName)
                            .addComponent(txtAddStreet)
                            .addComponent(txtAddCity, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtAddState)
                            .addComponent(txtAddZip)
                            .addComponent(txtAddCountry)
                            .addComponent(txtHomePhone)
                            .addComponent(txtWorkPhone)
                            .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 352, Short.MAX_VALUE)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnEdit)
                        .addGap(160, 160, 160)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtLastVisited)
                    .addComponent(txtCCNumber)
                    .addComponent(dtCCExpiry, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addGap(415, 415, 415)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8)
                            .addComponent(jLabel14)
                            .addComponent(txtCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAddState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtLastVisited, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel9)
                            .addComponent(jLabel15)
                            .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAddZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCCNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel10)
                            .addComponent(jLabel16)
                            .addComponent(txtMiddleName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAddCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(dtCCExpiry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel11)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHomePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel12)
                    .addComponent(txtAddStreet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtWorkPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel13)
                    .addComponent(txtAddCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(90, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDeleteMouseClicked
        // TODO add your handling code here:
         int rowIndex = tblCustomerMgmt.getSelectedRow();
        //
        String strID = (String) tblCustomerMgmt.getValueAt(rowIndex, 0);
        int id = Integer.parseInt(strID);
        
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int i = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa khach hang");
        if (i == JOptionPane.YES_OPTION) {
            
                CustomerDAO.getInstance().deleteCustomer(id);
            
            JOptionPane.showMessageDialog(this, "Đã xóa KH");
            loadTableCustomer(refreshCustomerList());
        } else {
            remove(dialogButton);
        }
    }//GEN-LAST:event_btnDeleteMouseClicked

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        // TODO add your handling code here:
        resetComponent();
        setEnableComponent(true);
    }//GEN-LAST:event_btnAddMouseClicked

    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEditMouseClicked
        // TODO add your handling code here:
        setEnableComponent(true);
    }//GEN-LAST:event_btnEditMouseClicked

    private void tblCustomerMgmtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblCustomerMgmtKeyReleased
        // TODO add your handling code here:
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        binding();
    }//GEN-LAST:event_tblCustomerMgmtKeyReleased

    private void btnCanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCanelMouseClicked
        // TODO add your handling code here:
        setEnableComponent(false);
    }//GEN-LAST:event_btnCanelMouseClicked

    private void btnSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSaveMouseClicked
        // TODO add your handling code here:
        String firstName= txtFirstName.getText();
        String middleName = txtMiddleName.getText();
        String lastName = txtLastName.getText();
        String street = txtAddStreet.getText();
        String city = txtAddCity.getText();
        String state = txtAddState.getText();
        
        String zip = txtAddZip.getText();
        int zip1 = Integer.parseInt(zip);
        
        String countTry = txtAddCountry.getText();
        
        String homePhone = txtHomePhone.getText();
        int homp = Integer.parseInt(homePhone);
        
        String workPhone = txtWorkPhone.getText();
        int wkphone = Integer.parseInt(workPhone);
        
        String email = txtEmail.getText();
        
        String lastv = txtLastVisited.getText();
        int latv = Integer.parseInt(lastv);
        
        String ccNumber = txtCCNumber.getText();
        int ccn = Integer.parseInt(ccNumber);
        
        java.util.Date ccExpr = dtCCExpiry.getDate();
        java.sql.Date cce = new java.sql.Date(ccExpr.getTime());



        
        if (txtCustomerID.getText().equals("")) {
            Customer ctm = new Customer(firstName, middleName, lastName, street, city, state, zip1, countTry, homp, wkphone, email, latv, ccn, cce, true, null);
            if (CustomerDAO.getInstance().createCustomer(ctm)) {
                JOptionPane.showMessageDialog(null, "Thêm mới thành công");
            } else {
                JOptionPane.showMessageDialog(null, "Lỗi");
            }
            resetComponent();
            setEnableComponent(false);
            loadTableCustomer(refreshCustomerList());
        } else {
            String strIDCtm = txtCustomerID.getText();
            int id = Integer.parseInt(strIDCtm);
            Customer customer = new Customer(id, firstName, middleName, lastName, street, city, state, zip1, countTry, homp, wkphone, email, latv, ccn, cce, true, null);
            if (CustomerDAO.getInstance().updateCustomer(customer)) {
                JOptionPane.showMessageDialog(null, "Cập nhật thành công");
            }else{
                JOptionPane.showMessageDialog(null, "Có lỗi xảy ra");
            }
            resetComponent();
            loadTableCustomer(refreshCustomerList());
            setEnableComponent(false);
        }
    }//GEN-LAST:event_btnSaveMouseClicked

    private void tblCustomerMgmtMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustomerMgmtMouseClicked
        // TODO add your handling code here:
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        binding();
    }//GEN-LAST:event_tblCustomerMgmtMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCanel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private com.toedter.calendar.JDateChooser dtCCExpiry;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblCustomerMgmt;
    private javax.swing.JTextField txtAddCity;
    private javax.swing.JTextField txtAddCountry;
    private javax.swing.JTextField txtAddState;
    private javax.swing.JTextField txtAddStreet;
    private javax.swing.JTextField txtAddZip;
    private javax.swing.JTextField txtCCNumber;
    private javax.swing.JTextField txtCustomerID;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtHomePhone;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtLastVisited;
    private javax.swing.JTextField txtMiddleName;
    private javax.swing.JTextField txtWorkPhone;
    // End of variables declaration//GEN-END:variables
}
